package com.zy.xuanwo.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zy.xuanwo.R;
import com.zy.xuanwo.acitivity.ConfigurationWiFiActivity;
import com.zy.xuanwo.acitivity.RemoteControlActivity;
import com.zy.xuanwo.acitivity.SocketActivity;
import com.zy.xuanwo.view.CustomerPopupWindow;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @功能 设备
 * Created by njb on 2016/7/3.
 */
public class DeviceFragment extends Fragment implements View.OnClickListener {

    private View mRootView;
    private Context mContext;
    @Bind(R.id.right_sliding_menu_iv)
    ImageView mAddView;
    private CustomerPopupWindow pw = null;
    private ImageView currentViewBar;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_device, null);
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        ButterKnife.bind(this, mRootView);
        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (pw != null) {
            pw.showPopupWindow(currentViewBar);
            pw.dismiss();
            Log.e("log-OnResume-fragment", "currentViewBar:" + currentViewBar);
        }
        Log.e("pw", "pw:" + pw);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.right_sliding_menu_iv)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_sliding_menu_iv:
                if (pw == null || !pw.isShowing()) {
                    pw = new DevicePopupWindow(getActivity());
                    currentViewBar = mAddView;
                    pw.showPopupWindow(currentViewBar);
                }else {
                    pw = null;
                    pw.dismiss();
                }
                break;
            default:
                pw = null;
                currentViewBar = null;
                break;
        }
    }

    public class DevicePopupWindow extends CustomerPopupWindow {
        private View mView;

        public DevicePopupWindow(final Activity context) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = inflater.inflate(R.layout.popupwindow_device, null);

            int h = context.getWindowManager().getDefaultDisplay().getHeight();
            int w = context.getWindowManager().getDefaultDisplay().getWidth();
            // 设置SelectPicPopupWindow的View
            this.setContentView(mView);
            // 设置SelectPicPopupWindow弹出窗体的宽
            this.setWidth(w);
            // 设置SelectPicPopupWindow弹出窗体的高
            this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            //不设置这一句，PopupWindow会遮挡系统底部按键
            this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            // 设置SelectPicPopupWindow弹出窗体可点击
            this.setFocusable(true);
            this.setOutsideTouchable(true);
            // 刷新状态
            this.update();
            // 实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(0000000000);
            // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
            this.setBackgroundDrawable(dw);
            // 设置SelectPicPopupWindow弹出窗体动画效果
            this.setAnimationStyle(R.style.AnimationPreview);
            final LinearLayout smartrl = (LinearLayout) mView.findViewById(R.id.smart_socket_ll);
            final LinearLayout control = (LinearLayout) mView.findViewById(R.id.remote_control_ll);

            smartrl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(),SocketActivity.class));
                }
            });

            control.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), RemoteControlActivity.class));
                }
            });

        }

        public void showPopupWindow(View parent) {
            if (!this.isShowing()) {
                this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 1);
            } else {
                this.dismiss();
            }
        }
    }


}
